/**
 * Created by lwy on 2017/8/16.
 */

var goodsCategoryLevel;
var page = 1;

//获取父菜单
$(function(){
    doAjax();
})


function doAjax(){
    $.ajax({
        url:'/crm/periodC/search.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            pageNu:page,
            goodsName:$('#goodsName').val(),
            code:$('#code').val(),
            state:$('#state').val(),
        },
        type:'post',
        success: function (data) {
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                $('#record').html(data.datas.recordsTotal);
                $('#withdraw-record').empty();
                for(var i=0;i<data.datas.periodList.length;i++){
                    $('<tr class="gradeA odd">'+
                        '<a href=""><td class="sorting_1 withdrawId" style="text-decoration: underline;cursor: pointer;" id="'+data.datas.periodList[i].id+'">'+data.datas.periodList[i].id+'</td></a>'+
                        '<td class="sorting_1">'+data.datas.periodList[i].unitPrice+'</td>'+
                        '<td class="center ">'+data.datas.periodList[i].nowPeople+'</td>'+
                        '<td class=" ">'+data.datas.periodList[i].beginTime+'</td>'+
                        '<td class=" ">'+data.datas.periodList[i].endTime+'</td>'+
                        '<td class=" ">'+data.datas.periodList[i].goodsName+'</td>'+
                        '<td class=" ">'+data.datas.periodList[i].state+'</td>'+
                        '</tr>').appendTo($('#withdraw-record'));
                }

                $(".pagination").createPage({
                    totalPage:data.datas.totalPage,
                    currPage:data.datas.pageNow,
                    pageCount:data.datas.pageNow,
                    turndown:'true',
                    backFn:function(p){
                        page=p;
                        doAjax();
                    }
                });
            }
        }
    })
}

//查询
$('#refer').click(function(){
    page=1;
    doAjax();
})

//添加时选择父级菜单
$('#addType').change(function(){
    if($('#addType').val()==2||$('#addType').val()==3){
        $('#addfaBox').show();
        $.ajax({
            url:'/crm/goodsCategoryC/getSuper',
            dataType:'json',
            data:{
                goodsCategoryLevel:$('#addType').val(),
            },
            type:'post',
            success:function(data){
                if(data.code==1){
                    $('#addfaMenu').empty();
                    $('<option value="">请选择</option>').appendTo($('#addfaMenu'));
                    for(var i=0;i<data.datas.length;i++){
                        $('<option value="'+data.datas[i].id+'">'+data.datas[i].name+'</option>').appendTo($('#addfaMenu'));
                    }
                }
            }
        })
    }else{
        $('#addfaBox').hide();
        $('#addfaMenu').empty()
        $('<option value="0"></option>').appendTo($('#addfaMenu'));
    }
})


//添加
$('#save').click(function(){
    $.ajax({
        url:'/crm/goodsCategoryC/addGoodsCategory',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            name:$('#name').val(),
            imgUrl:$('#imgurl').val(),
            goodsCategoryLevel:$('#addType').val(),
            parentCategoryId:$('#addfaMenu').val(),
            summary:$('#explain').val(),
            number:$('#number').val(),
        },
        type:'post',
        success:function(data){
            if(data.code==0){
                alert(data.msg)
                return;
            }
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==1){
                alert('添加成功');
                $('#myModal5').removeClass('fade in');
                $('#myModal5').addClass('fade');
                $('#myModal5').attr('aria-hidden',true);
                $('#myModal5').css('display','none');
                $('.modal-backdrop').css('display','none');
                $('#myModal5 input').val('');
                page=1;
                doAjax();
            }
        }
    })
})
